package com.freesky.provider.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.freesky.common.dao.entity.HomeNav;
import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.exception.MyException;
import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.TimeUtil;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.provider.content.service.IHomeNavigationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Freesky
 */
@Service
@Slf4j
public class HomeNavigationServiceImpl implements IHomeNavigationService {
    @Autowired
    private IRedisTemplate redisTemplate;

    @Override
    public JSONResponse getPortalNavList() {

        //查询缓存
        List<HomeNav> list = redisTemplate.getCacheList(RedisKeyEnum.HOME_NAV.getKey(), HomeNav.class);
        if (list != null) {
            log.info("读取了导航栏缓存");
            return JSONResponseDiretor.buildSuccessJSONResponse(list);
        }
        list = new HomeNav().selectList(new EntityWrapper().orderBy("sort_order", true));
        //把结果添加至缓存
        if (list != null) {
            redisTemplate.putCache(RedisKeyEnum.HOME_NAV.getKey(), JSON.toJSON(list));
            log.info("添加了导航栏缓存");
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(list);
    }

    @Override
    public JSONResponse add(HomeNav homeNav) {

        homeNav.setCreateTime(TimeUtil.getDateTimeNow());
        homeNav.setUpdateTime(TimeUtil.getDateTimeNow());
        try {
            homeNav.insert();
        } catch (Exception e) {
            throw new MyException(ResponseEnum.DELETE_HOME_NAV_ERROR,e,null);
        }

        redisTemplate.delete(RedisKeyEnum.HOME_NAV.getKey());
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse delete(int[] ids) {
        if (ids == null) {
            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        }
        for (int id : ids) {
            HomeNav homeNav = new HomeNav(id);
            try {
                homeNav.deleteById();
            } catch (Exception e) {
                throw new MyException(ResponseEnum.DELETE_HOME_NAV_ERROR,e,null);
            }
            redisTemplate.delete(RedisKeyEnum.HOME_NAV.getKey());
        }

        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }
    @Override
    public JSONResponse update(HomeNav homeNav) {
        homeNav.setUpdateTime(TimeUtil.getDateTimeNow());
        try {
            homeNav.updateById();
        } catch (Exception e) {
            throw new MyException(ResponseEnum.UPDATE_HOME_NAV_ERROR,e,null);
        }
        redisTemplate.delete(RedisKeyEnum.HOME_NAV.getKey());
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public DataTablesResult list() {
        List<HomeNav> list = new HomeNav().selectList(new EntityWrapper().orderBy("sort_order", true));
        DataTablesResult result = new DataTablesResult();
        result.setData(list);
        return result;
    }
}
